package com.probox.modules.controller.product;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.product.ProductInfoGroup;
import com.probox.common.entity.service.product.IProductInfoGroupService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductGroup;
import com.probox.common.entity.domain.product.vo.ProductGroupQuery;
import com.probox.common.entity.domain.product.vo.ProductGroupVo;
import com.probox.common.entity.service.product.IProductGroupService;
import com.probox.product.api.domain.ProductGroupViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品分组信息Controller
 *
 * @date 2021-06-03
 */
@RestController
@RequestMapping("/product/group")
public class ProductGroupController extends BaseController {
    @Autowired
    private IProductGroupService productGroupService;

    @Autowired
    private IProductInfoGroupService productInfoGroupService;

    /**
     * 查询商品分组信息列表
     */
    @PreAuthorize(hasPermi = "product:group:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductGroup productGroup) {
        //startPage();
        List<ProductGroup> list = productGroupService.queryList(productGroup);
        return getDataTable(list);
    }


    @GetMapping("/firstGroupList")
    public AjaxResult firstGroupList(ProductGroup productGroup) {
        productGroup.setDataStatus(DataStatus.valid.getKey());
        productGroup.setParentGroupUid(0L);
        return AjaxResult.success(productGroupService.queryList(productGroup));
    }


    @GetMapping("/selectList")
    public AjaxResult selectList(ProductGroup productGroup) {
        productGroup.setDataStatus(DataStatus.valid.getKey());
        return AjaxResult.success(productGroupService.queryList(productGroup));
    }

    @GetMapping("/listPage")
    public AjaxResult listP(ProductGroupQuery productGroupQuery) {
        startPage();
        List<ProductGroupViewVo> productGroupViewVos = productGroupService.selectGroupP(productGroupQuery);
        return AjaxResult.successs(productGroupViewVos, productGroupService.selectGroupCountP(productGroupQuery));
    }

    /**
     * 导出商品分组信息列表
     */
    @PreAuthorize(hasPermi = "product:group:export")
    @Log(title = "论文分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductGroup productGroup) throws IOException {
        List<ProductGroup> list = productGroupService.queryList(productGroup);
        ExcelUtil<ProductGroup> util = new ExcelUtil<ProductGroup>(ProductGroup.class);
        util.exportExcel(response, list, "类型信息数据");
    }

    /**
     * 获取商品分组信息详细信息
     */
    @PreAuthorize(hasPermi = "product:group:query")
    @GetMapping(value = "/{groupUid}")
    public AjaxResult getInfo(@PathVariable("groupUid") Long groupUid) {
        return AjaxResult.success(productGroupService.selectProductGroupById(groupUid));
    }


    @GetMapping("/listByType/{groupUid}/{type}")
    public AjaxResult listByType(@PathVariable("groupUid") Long groupUid, @PathVariable String type) {
        return productGroupService.listByType(groupUid, type);
    }


    /**
     * 新增商品分组信息
     */
    @PreAuthorize(hasPermi = "product:group:add")
    @Log(title = "论文分类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductGroupVo productGroup) {
        return toAjax(productGroupService.insertProductGroup(productGroup));
    }

    /**
     * 修改商品分组信息
     */
    @PreAuthorize(hasPermi = "product:group:edit")
    @Log(title = "论文分类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductGroupVo productGroupVo) {
        return toAjax(productGroupService.updateProductGroup(productGroupVo));
    }

    /**
     * 删除商品分组信息
     */
    @PreAuthorize(hasPermi = "product:group:remove")
    @Log(title = "论文分类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupUids}")
    public AjaxResult remove(@PathVariable Long[] groupUids) {
        return toAjax(productGroupService.deleteProductGroupByIds(groupUids));
    }

    /**
     * 修改是否推荐
     * @param group
     * @return
     */
    @PostMapping("/updateRecommend")
    public AjaxResult updateRecommend(@RequestBody ProductInfoGroup group) {
        return AjaxResult.success(productInfoGroupService.updateRecommend(group));
    }

}
